﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using HtmlAgilityPack;
using System.Globalization;

namespace Parsowanie
{
    public static class HtmlSecurityParser
    {
        public static List<SecurityIndice> GetIndeces(string html)
        {
          
            var listSecurityIndice = new List<SecurityIndice>();

            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            var collectionTr = doc.DocumentNode.SelectNodes("//tr").
                Where(item => item.Attributes["id"] != null && item.Attributes["id"].Value.Equals("noto"));

            foreach (var tr in collectionTr)
            {
                SecurityIndice newSecurityIndice = new SecurityIndice();

                newSecurityIndice.Security.SecurityName = tr.ChildNodes[1].InnerText;
                newSecurityIndice.Security.SecurityCode = tr.ChildNodes[3].InnerText;
                newSecurityIndice.Rate = float.Parse(tr.ChildNodes[5].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.RateChange = float.Parse(tr.ChildNodes[7].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.PercentRateChange = float.Parse(tr.ChildNodes[9].InnerText.Substring(0,tr.ChildNodes[9].InnerText.Length-1), CultureInfo.InvariantCulture);
                newSecurityIndice.TradingAmount = float.Parse(tr.ChildNodes[11].InnerText.Replace("&nbsp;", ""),CultureInfo.InvariantCulture);
                newSecurityIndice.Trading = float.Parse(tr.ChildNodes[13].InnerText.Replace("&nbsp;", ""), CultureInfo.InvariantCulture);
                newSecurityIndice.RateOpen = float.Parse(tr.ChildNodes[15].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.RateMax = float.Parse(tr.ChildNodes[17].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.RateMin = float.Parse(tr.ChildNodes[19].InnerText, CultureInfo.InvariantCulture);


                listSecurityIndice.Add(newSecurityIndice);
            }


            return listSecurityIndice;
        }

        public static List<SecurityIndice> GetObligacje(string  html)
        {
            List<SecurityIndice> listSecurityIndice = new List<SecurityIndice>();

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);

            var collectionTr = doc.DocumentNode.SelectNodes("//tr").
                Where(item => item.Attributes["id"] != null && item.Attributes["id"].Value.Equals("noto"));

            foreach (var tr in collectionTr)
            {
                SecurityIndice newSecurityIndice = new SecurityIndice();

                newSecurityIndice.Security.SecurityName = tr.ChildNodes[1].InnerText.Replace("&nbsp;", " ");
                newSecurityIndice.Termin = tr.ChildNodes[3].InnerText;
                newSecurityIndice.Rate = float.Parse(tr.ChildNodes[5].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.RateChange = string.IsNullOrWhiteSpace(tr.ChildNodes[7].InnerText) ? 0f : float.Parse(tr.ChildNodes[7].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.PercentRateChange = float.Parse(tr.ChildNodes[9].InnerText.Substring(0, tr.ChildNodes[9].InnerText.Length - 1), CultureInfo.InvariantCulture);
                newSecurityIndice.RateOpen = string.IsNullOrWhiteSpace(tr.ChildNodes[11].InnerText) ? 0f : float.Parse(tr.ChildNodes[11].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.RateMax = string.IsNullOrWhiteSpace(tr.ChildNodes[13].InnerText) ? 0f : float.Parse(tr.ChildNodes[13].InnerText, CultureInfo.InvariantCulture);
                newSecurityIndice.RateMin = string.IsNullOrWhiteSpace(tr.ChildNodes[15].InnerText) ? 0f : float.Parse(tr.ChildNodes[15].InnerText, CultureInfo.InvariantCulture);


                listSecurityIndice.Add(newSecurityIndice);
            }


            return listSecurityIndice;
        }



    }
}

